/*
 * Copyright 2015-2016 Tumblr, Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.tumblr.backboard.imitator;

import com.facebook.rebound.Spring;
import com.tumblr.backboard.MotionProperty;
import com.tumblr.backboard.Utils.MathUtils;
import ohos.agp.components.Component;
import ohos.multimodalinput.event.TouchEvent;

/**
 * Maps a user's motion to a Component via a Spring.
 *
 * @author ericleong
 * @since 2021-05-07
 */
public class MotionImitator extends EventImitator {
    /**
     * The motion property to imitate.
     */
    protected MotionProperty mProperty;

    /**
     * Used internally to keep track of the initial down position.
     */
    protected float mDownPosition;

    /**
     * The offset between the view left/right location and the desired "center" of the view.
     */
    protected float mOffset;

    /**
     * Constructor. Uses {@link #TRACK_ABSOLUTE} and {@link #FOLLOW_EXACT}.
     *
     * @param property the property to track.
     */
    public MotionImitator(MotionProperty property) {
        this(null, property, 0, TRACK_ABSOLUTE, FOLLOW_EXACT);
    }

    /**
     * Constructor. It is necessary to call {@link #setSpring(Spring)} to set the spring.
     *
     * @param property the property to track.
     * @param trackStrategy the tracking strategy.
     * @param followStrategy the follow strategy.
     */
    public MotionImitator(MotionProperty property, int trackStrategy, int followStrategy) {
        this(null, property, 0, trackStrategy, followStrategy);
    }

    /**
     * Constructor. Uses {@link #TRACK_ABSOLUTE} and {@link #FOLLOW_EXACT}.
     *
     * @param spring the spring to use.
     * @param property the property to track.
     */
    public MotionImitator(Spring spring, MotionProperty property) {
        this(spring, property, spring.getEndValue(), TRACK_ABSOLUTE, FOLLOW_EXACT);
    }

    /**
     * Constructor. Uses {@link #TRACK_ABSOLUTE} and {@link #FOLLOW_EXACT}.
     *
     * @param spring the spring to use.
     * @param property the property to track.
     * @param restValue the rest value for the spring.
     */
    public MotionImitator(Spring spring, MotionProperty property, double restValue) {
        this(spring, property, restValue, TRACK_ABSOLUTE, FOLLOW_EXACT);
    }

    /**
     * Constructor.
     *
     * @param spring the spring to use.
     * @param property the property to track.
     * @param restValue the rest value for the spring.
     * @param trackStrategy the tracking strategy.
     * @param followStrategy the follow strategy.
     */
    public MotionImitator(Spring spring, MotionProperty property, double restValue, int trackStrategy,
            int followStrategy) {
        super(spring, restValue, trackStrategy, followStrategy);
        mProperty = property;
    }

    /**
     * Constructor.
     *
     * @param property the property to track.
     * @param restValue the rest value for the spring.
     * @param trackStrategy the tracking strategy.
     * @param followStrategy the follow strategy.
     */
    public MotionImitator(MotionProperty property, double restValue, int trackStrategy, int followStrategy) {
        super(restValue, trackStrategy, followStrategy);
        mProperty = property;
    }

    /**
     * setRestValue
     *
     * @param restValue restValue
     */
    public void setRestValue(double restValue) {
        this.mRestValue = restValue;
    }

    /**
     * Puts the spring to rest.
     *
     * @return this object for chaining.
     */
    public MotionImitator rest() {
        if (mSpring != null) {
            mSpring.setEndValue(mRestValue);
        }

        return this;
    }

    @Override
    public void constrain(TouchEvent event) {
        super.constrain(event);

        mDownPosition = MathUtils.floatToAdd(mProperty.getValue(event), mOffset);
    }

    @Override
    public void imitate(Component view, TouchEvent event) {
        float viewValue = mProperty.getValue(view);
        float eventValue = mProperty.getValue(event);
        mOffset = mProperty.getOffset(view);

        if (event.getPointerCount() > 0) {
            float historicalValue = mProperty.getOldestValue(event);
            imitate(MathUtils.floatToAdd(viewValue, mOffset), eventValue,
                    MathUtils.floatToSubtract(eventValue, historicalValue), event);
        } else {
            imitate(MathUtils.floatToAdd(viewValue, mOffset), eventValue, 0, event);
        }
    }

    @Override
    public void mime(float offset, float value, float delta, float dt, TouchEvent event) {
        if (mTrackStrategy == TRACK_DELTA) {
            super.mime(MathUtils.floatToSubtract(offset, mDownPosition), value, delta, dt, event);
        } else {
            super.mime(offset, value, delta, dt, event);
        }
    }

    @Override
    protected double mapToSpring(float motionValue) {
        return motionValue;
    }

    /**
     * getProperty
     *
     * @return mProperty
     */
    public MotionProperty getProperty() {
        return mProperty;
    }
}
